package com.yds.ydsdatasetbackend.file;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.yds.ydsdatasetbackend.constant.FileConstant;
import com.yds.ydsdatasetbackend.exception.BusinessException;
import com.yds.ydsdatasetbackend.exception.ErrorCode;
import com.yds.ydsdatasetbackend.model.dto.file.message.GetFileMessageRequest;
import com.yds.ydsdatasetbackend.model.dto.file.message.GetFileMessageResponse;
import com.yds.ydsdatasetbackend.model.dto.file.predict.remote.RemoteFilePredictAddRequest;
import com.yds.ydsdatasetbackend.model.dto.file.train.remote.RemoteFileTrainAddRequest;
import com.yds.ydsdatasetbackend.model.enums.FileModelEnum;
import com.yds.ydsdatasetbackend.model.enums.FileTrainEnum;
import com.yds.ydsdatasetbackend.model.vo.TrainResult;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;


public class RemoteInteractionImp implements RemoteInteraction{

    // 定义鉴权
    private static final String AUTH_REQUEST_HEADER = "auth";
    private static final String AUTH_REQUEST_SECRET = "secretKey";

    @Override
    public GetFileMessageResponse executeCode(GetFileMessageRequest getFileMessageRequest) {
        System.out.println("远程文件信息获取");
        String url = "http://localhost:8124/file/info";
        String json = JSONUtil.toJsonStr(getFileMessageRequest);
        System.out.println(json);
        String responseStr = HttpUtil.createPost(url)
                .header(AUTH_REQUEST_HEADER,AUTH_REQUEST_SECRET)
                .header("Content-Type", "application/json")
                .body(json)
                .execute()
                .body();
        if(StringUtils.isBlank(responseStr)) {
            throw new BusinessException(ErrorCode.API_REQUEST_ERROR, "executeCode remoteSandbox error message = {}" + responseStr);
        }
        return JSONUtil.toBean(responseStr, GetFileMessageResponse.class);
    }

    @Override
    public String doTrain(RemoteFileTrainAddRequest remoteFileTrainAddRequest) {
        System.out.println("远程文件进行训练");
        String url = "http://localhost:8124/file/train";
        String json = JSONUtil.toJsonStr(remoteFileTrainAddRequest);
        System.out.println(json);
        String responseStr = HttpUtil.createPost(url)
                .header(AUTH_REQUEST_HEADER,AUTH_REQUEST_SECRET)
                .header("Content-Type", "application/json")
                .body(json)
                .execute()
                .body();
        return responseStr;
    }

    @Override
    public String doPredict(RemoteFilePredictAddRequest remoteFilePredictAddRequest) {
        System.out.println("远程文件进行预测");
        String url = "http://localhost:8124/file/predict";
        String json = JSONUtil.toJsonStr(remoteFilePredictAddRequest);
        System.out.println(json);
        String responseStr = HttpUtil.createPost(url)
                .header(AUTH_REQUEST_HEADER,AUTH_REQUEST_SECRET)
                .header("Content-Type", "application/json")
                .body(json)
                .execute()
                .body();
        return responseStr;
    }


    public static void main(String[] args) {
//        GetFileMessageRequest getFileMessageRequest = new GetFileMessageRequest();
//        getFileMessageRequest.setUrl("https://yds-dataset-1325553198.cos.ap-shanghai.myqcloud.com/public/1884211650339426305/2025-02-02_4pvkry96oz8pg5qo.csv");
//        RemoteInteraction remoteInteraction = new RemoteInteractionImp();
//        GetFileMessageResponse getFileMessageResponse = remoteInteraction.executeCode(getFileMessageRequest);
//        System.out.println(getFileMessageResponse);

//        RemoteFileTrainAddRequest remoteFileTrainAddRequest = new RemoteFileTrainAddRequest();
//        remoteFileTrainAddRequest.setId(1234567890L);
//        remoteFileTrainAddRequest.setUrl(FileConstant.COS_HOST + "/public/1884211650339426305/2025-02-07_jzauogdtv8b1v662.csv");
//        remoteFileTrainAddRequest.setModel(FileModelEnum.KNN.getValue());
//        remoteFileTrainAddRequest.setTrainRate(70L);
//        remoteFileTrainAddRequest.setIsRefresh(1);
//        List<Long> selectFeatures = new ArrayList<>();
//        for (int i = 0; i < 12; i++) {
//            selectFeatures.add((long) i);
//        }
//        List<Long> selectTarget = new ArrayList<>();
//        selectTarget.add(12L);
//        remoteFileTrainAddRequest.setSelectFeatures(selectFeatures);
//        remoteFileTrainAddRequest.setSelectTargets(selectTarget);
//        remoteFileTrainAddRequest.setFileId(2222222L);
//        remoteFileTrainAddRequest.setUserId(1111111L);
//        RemoteInteraction remoteInteraction = new RemoteInteractionImp();
//        String responseStr = remoteInteraction.doTrain(remoteFileTrainAddRequest);
//        if(StringUtils.isBlank(responseStr)) {
//            throw new BusinessException(ErrorCode.API_REQUEST_ERROR, "doTrain remoteTrain error message = {}" + responseStr);
//        }
//        System.out.println(responseStr);
//        TrainResult trainResult = JSONUtil.toBean(responseStr, TrainResult.class);
//        System.out.println(JSONUtil.toJsonStr(trainResult));


        RemoteFilePredictAddRequest remoteFilePredictAddRequest = new RemoteFilePredictAddRequest();
        remoteFilePredictAddRequest.setUserId(1884211650339426305L);
        remoteFilePredictAddRequest.setFileId(1887752739488174081L);
        remoteFilePredictAddRequest.setTrainId(1888448488645050369L);
        remoteFilePredictAddRequest.setModel("KNN");
        List<Double> numbers = new ArrayList<>();

        // 添加给定的数值
        numbers.add(7.50e+01);   // 75.0
        numbers.add(1.00e+00);   // 1.0
        numbers.add(2.46e+02);   // 246.0
        numbers.add(0.00e+00);   // 0.0
        numbers.add(1.50e+01);   // 15.0
        numbers.add(0.00e+00);   // 0.0
        numbers.add(1.27e+05);   // 127000.0
        numbers.add(1.20e+00);   // 1.2
        numbers.add(1.37e+02);   // 137.0
        numbers.add(1.00e+00);   // 1.0
        numbers.add(0.00e+00);   // 0.0
        numbers.add(1.00e+01);   // 10.0
        remoteFilePredictAddRequest.setParams(numbers);
        // 向python端发送请求
        RemoteInteraction remoteInteraction = new RemoteInteractionImp();
        String responseStr = remoteInteraction.doPredict(remoteFilePredictAddRequest);
        System.out.println(responseStr);

    }
}
